﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using TestLogRealtimeMonitor.Models;

namespace TestLogRealtimeMonitor.ViewModels
{
    public class SelectProductDialogViewModel : BindableBase, IDialogAware
    {
        #region 属性
        private ObservableCollection<ItemsContentEntity> products;
        /// <summary>
        /// 产品集
        /// </summary>
        public ObservableCollection<ItemsContentEntity> Products
        {
            get { return products; }
            set { SetProperty(ref products, value); }
        }
        private string selectedProduct;
        /// <summary>
        /// 选择的产品
        /// </summary>
        public string SelectedProduct
        {
            get { return selectedProduct; }
            set { SetProperty(ref selectedProduct, value); }
        }
        #endregion

        #region 命令
        private DelegateCommand<string> lstProductMouseDoubleClick;
        public DelegateCommand<string> LstProductMouseDoubleClick =>
            lstProductMouseDoubleClick ?? (lstProductMouseDoubleClick = new DelegateCommand<string>(LstProductMouseDoubleClickHandler));

        #endregion
        public string Title => "";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters!=null)
            {
              var receiveProducts=  parameters.GetValue<Dictionary<string, string>>("Products");
              _=AddProductsToListBoxControlsAsync(receiveProducts);
            }
        }
        protected virtual void CloseDialog(string parameter)
        {
            ButtonResult result = ButtonResult.None;

            if (parameter?.ToLower() == "true")
                result = ButtonResult.OK;
            else if (parameter?.ToLower() == "false")
                result = ButtonResult.Cancel;

            RaiseRequestClose(new DialogResult(result));
        }
        public virtual void RaiseRequestClose(IDialogResult dialogResult)
        {
            RequestClose?.Invoke(dialogResult);
        }
        #region 方法
        void LstProductMouseDoubleClickHandler(string parameter)
        {
            if (parameter!=null)
            {
                RaiseRequestClose(new DialogResult(ButtonResult.OK, new DialogParameters($"SelectedProductFullPath={parameter}")));
            }
        }
        private async Task AddProductsToListBoxControlsAsync(Dictionary<string, string> products)
        {
            Products = new ObservableCollection<ItemsContentEntity>();
            await Task.Run(() => {
                foreach (var p in products)
                {
                    Application.Current.Dispatcher.Invoke(() => {
                        Products.Add(new ItemsContentEntity { Title = p.Key, Value = p.Value });
                    });
                }
            });
        }
        #endregion
    }
}
